package com.naiterui.ehp.bs.doctor.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BaseExceptionCodes;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.doctor.exception.ExceptionCodes;
import com.naiterui.ehp.bs.doctor.service.IIndexService;
import com.naiterui.ehp.bs.doctor.utils.PropertyValueConstants;
import com.naiterui.ehp.bs.doctor.utils.RedisUtils;
import com.naiterui.ehp.bs.doctor.vo.BaseInfoVO;
import com.naiterui.ehp.bs.doctor.vo.SpreadVo;
import com.naiterui.ehp.bs.doctor.vo.StartInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.constraints.NotNull;

@Validated
@Controller
@RequestMapping("ad")
@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.USER_INFO})
public class AdIndexController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AdIndexController.class);

    @Autowired
    private IIndexService indexService;

    /**
     * <首页推广码>
     * <详细描述>
     *
     * @param doctorId
     *
     * @return String <返回值描述>
     *
     * @Throws 异常信息
     * @History 2015年6月26日 上午10:38:05 by chenlin
     */
    @ApiOperation("获取推广二维码")
    @RequestMapping(value = "/spreadCode", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public SpreadVo spreadCode(@NotNull(message = "医生Id不能为null") Long doctorId) throws BusinessException {
        return this.indexService.findSpreadByDoctorId(doctorId);

    }


    /**
     * 医生APP端首页数据接口
     * （医生端优化1.1）新增参数
     *  累计处方数累 medicationRecomNum
     *  计问诊数量 consultNum
     */
    @ResponseBody
    @RequestMapping("index/baseInfo")
    @ApiOperation(value = "医生APP端首页数据接口", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "long", paramType = "query")})
    public ResponseVO<BaseInfoVO> baseInfo(@NotNull(message = "医生Id不能为null") Long doctorId) throws JsonProcessingException, BusinessException {
        LOGGER.info("医生APP端首页数据接口 请求参数：doctorId = [{}]", doctorId);
        if (doctorId == null) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }

        BaseInfoVO baseInfoVO = this.indexService.getIndexBaseInfo(doctorId);

        return PublicService.returnResponseVO(baseInfoVO);
    }

    /**
     * 医生APP端启动页数据接口(2.17新增)
     */
    @ResponseBody
    @RequestMapping("index/startInfo")
    @ApiOperation(value = "医生APP端启动页数据接口[2.17新增]", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(name = "doctorId", value = "医生id", required = false, dataType = "long", paramType = "query")})
    public ResponseVO<StartInfoVO> startInfo(Long doctorId) throws BusinessException, Exception {
        LOGGER.info("医生APP端启动页数据接口，参数为：doctorId={}", doctorId);
        StartInfoVO startInfoVO = this.indexService.getStartInfo(doctorId);
        return PublicService.returnResponseVO(startInfoVO);

    }

    @ResponseBody
    @PostMapping("index/fastLogin/status")
    @ApiOperation("设置快捷登录状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId", value = "医生id", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态 1.已设置 2.下次不再提醒", required = true, dataType = "int", paramType = "query"),
    })
    public ResponseVO<Void> fastLoginStatus(Long doctorId, Integer status) throws BusinessException {
        LOGGER.info("设置快捷登录状态为不再提醒，参数为：doctorId={}, status: {}", doctorId, status);
        if(doctorId == null || status == null){
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        RedisUtils.set(PropertyValueConstants.FAST_LOGIN_STATUS_CACHE + doctorId, status);
        return PublicService.returnResponseVO(BaseExceptionCodes.SUCCESS);

    }
}
